#include "..\macro\stdafx.h"
#pragma once
#include "..\macro\macro.h"

#include "..\common\common.h"

namespace HUGEINT_H_NAMESPACE {
	void CMutex::lock ( ) {
		WaitForSingleObject ( m_handle , INFINITE );
	}

	void CMutex::unlock ( ) {
		ReleaseMutex ( m_handle );
	}

	CMutex::CMutex ( ) {
		m_handle = CreateMutex ( NULL , false , NULL );
		if (m_handle == 0) {
			THROWEXCEPTION ( *this , EXIT_CODE_INVALID_HANDLE );
		}
	}
	CMutex::~CMutex ( ) {
		if (m_handle)
			CloseHandle ( m_handle );
	}

	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

	//unsigned int CCondition_signal::wait_condition_signal ( unsigned int mark ) {
	//	return WaitForSingleObject ( m_handle , mark );
	//}

	//void CCondition_signal::set_condition_signal ( ) {
	//	ReleaseSemaphore ( m_handle , 1 , NULL );
	//}

	//CCondition_signal::CCondition_signal ( ) {
	//	m_handle = CreateSemaphore ( NULL , 0 , 1 , NULL );
	//	if (m_handle == 0) {
	//		THROWEXCEPTION ( *this , EXIT_CODE_INVALID_HANDLE );
	//	}
	//}

	//CCondition_signal::~CCondition_signal ( ) {
	//	if (m_handle) {
	//		CloseHandle ( m_handle );
	//	}
	//}

	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

	unsigned int CSemaphore::wait_semaphore ( unsigned int mark ) {
		return WaitForSingleObject ( m_handle , mark );
	}

	void CSemaphore::set_semaphore ( SIZE_T n ) {
		ReleaseSemaphore ( m_handle , n , NULL );
	}

	void CSemaphore::change_semaphore ( SIZE_T n ) {
		if (m_handle) {
			CloseHandle ( m_handle );
		}
		m_handle = CreateSemaphore ( NULL , 0 , n , NULL );
		if (m_handle == 0) {
			THROWEXCEPTION ( *this , EXIT_CODE_INVALID_HANDLE );
		}
	}
	CSemaphore::CSemaphore ( unsigned int n ) {
		m_handle = CreateSemaphore ( NULL , 0 , n , NULL );
		if (m_handle == 0) {
			THROWEXCEPTION ( *this , EXIT_CODE_INVALID_HANDLE );
		}
	}

	CSemaphore::~CSemaphore ( ) {
		if (m_handle) {
			CloseHandle ( m_handle );
		}
	}
}